package com.sangmo.fts.user.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.user.interfaces.RoleCreation;
import com.sangmo.fts.user.model.Role;
import com.sangmo.fts.user.repo.RoleRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RoleEntityService extends BaseViewService<Role> {

    @Autowired
    private RoleRepo roleRepo;

    @Override
    protected IDataViewRepo<Role> getRepo() {
        return roleRepo;
    }

    public Role findByExRoleId(String exRoleId) {
        return roleRepo.findByExRoleId(exRoleId);
    }

    public Role createIfNotExists(String exRoleId) {
        Role role = this.findByExRoleId(exRoleId);
        if (role == null) {
            RoleCreation creation = new RoleCreation();
            creation.setExRoleId(exRoleId);
            creation.setName(exRoleId);
            role = this.create(creation);
        }
        return role;
    }
}
